﻿<cfsilent>
	<cfscript>
		
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		mathAdvice = getProperty("serviceFactory").getBean("senateMathAdvice");
		
		response = structNew();
		structInsert(response, "success", true, true);
		structInsert(response, "reason", "没有适合的考场", true);
		
		progress = [];
		rooms = [];
		usageArray = [];
		
		termId = event.getArg("tid");
		
		weekId = event.getArg("wid");
		weekDay = event.getArg("wkday");
		cellIdx = event.getArg("lec");
		
		maxExamer = event.getArg("maxExamer");
		maxCourse = event.getArg("maxCourse");
		
		/* 读取执行场次的考试安排 */
		arrayAppend( progress, "正在读取第" & weekId & "周, 周" & weekDay & "第" & cellIdx & "场考场安排情况" );
		
		
		sql = "SELECT 
				d.institute_id, 
				e.campus_id, 
				d.course_name, 
				c.sits_left, 
				a.examer_sum, 
				c.course_count, 
				a.task_exrstid, 
				b.crtime_id, 
				b.cr_id, 
				e.classroom,
				d.course_name
				FROM 
					t_resittask a
					INNER JOIN t_course d ON d.cid = a.cid , 
					t_clsroom_timetable b 
					INNER JOIN t_clsroom_forresit c ON c.crtime_id = b.crtime_id 
					INNER JOIN t_classroom e ON e.cr_id = b.cr_id 
				WHERE 
					a.task_exrstid = c.task_exrstid 
					AND 
					a.term_id = :termId 
					AND 
					a.task_prog = '2' 
					AND 
					( a.examer_sum < :maxExamer or a.examer_sum = :maxExamer ) 
					AND 
					a.wid = :weekId 
					AND 
					a.wdidx = :weekDay 
					AND 
					a.cellidx = :cellIdx 
					AND 
					b.term_id = :termId 
					AND 
					c.course_count < :maxCourse 
				ORDER BY 
					d.institute_id, 
					e.campus_id, 
					c.sits_left DESC";
											
		queryObj = new Query( datasource=application.dnsSlave ) ;
		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="weekDay", value=weekDay, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="cellIdx", value=cellIdx, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="maxCourse", value=maxCourse, cfsqltype="cf_sql_integer" );
		queryObj.addParam( name="maxExamer", value=maxExamer, cfsqltype="cf_sql_integer" );
		queryObj.addParam( name="weekId", value=weekId, cfsqltype="cf_sql_char" );
		
		rs_usage= queryObj.execute( sql=sql ).getResult();
		
		for(  a=1; a LTE rs_usage.recordCount; a++){
			arrayAppend( usageArray, [rs_usage["institute_id"][a],rs_usage["campus_id"][a],rs_usage["course_name"][a], rs_usage["sits_left"][a],rs_usage["examer_sum"][a],rs_usage["course_count"][a],rs_usage["task_exrstid"][a],rs_usage["crtime_id"][a],rs_usage["cr_id"][a],rs_usage["classroom"][a],rs_usage["course_name"][a]] );
		}	
		
		arrayAppend( progress, "指定时段使用考场 " & arrayLen(usageArray) & " 个" );
		
			
		/* 向上叠加模式 即循环至倒数第二行及退出 */
			
		/* 数组内容 [ InstituteID, CampusID, CourseName, SitLeft, ExamSize, CourseCount, TaskResitExamID, ClassRoomTimeID, ClassroomID, ClsroomName, CourseNameList ] */
		
		if ( rs_usage.recordCount gt 1 ) {
			
			target = usageArray[1];
			
			arrayAppend( rooms, [ target[3], target[5], target[10], "" ] );
			
			transaction {
			
				for ( i=2; i lte rs_usage.recordCount; i++ ) {
				
					nextCombo = usageArray[i];

					if ( target[1] eq nextCombo[1] ) {
						/* 同任课单位 */
					
						if ( target[2] eq nextCombo[2] ) {
							/* 同校区 */
						
							if ( target[4] gte nextCombo[5] ) {
								/* 目标考场容量足够 */
								
								if ( target[6] lt maxCourse ) {
									/* 考场内课程门数小于上限 */

									/* 考场内课程增加 */
									target[6] = target[6]+1;
									/* 考场可用座次减少 */
									target[4] = target[4] - nextCombo[5];
									/* 课程列表更新 */
									if ( not listFind( target[11], nextCombo[3], "|" ) ) {
										target[11] = listAppend( target[11], nextCombo[3], "|" );
									}
								
									//arrayAppend( progress, "可将 " & nextCombo[3] & " 合并至 " & target[3] );
									arrayAppend( rooms, [ nextCombo[3], nextCombo[5], nextCombo[10], target[10], target[11] ] );

									
								
									sql = "SELECT a.sits_exam,
												   a.cr_id 
											FROM t_classroom a 
											WHERE a.cr_id = :classroomId ";
											
									queryObj = new Query( datasource=application.dnsMaster ) ;
									queryObj.addParam( name="classroomId", value=target[9], cfsqltype="cf_sql_char" );
									rs_classroom= queryObj.execute( sql=sql ).getResult();	

									/* 第一步 更新教室借用记录 */
									
									sql = "SELECT a.sits_left 
											FROM t_clsroom_forresit a 
											WHERE a.crtime_id = :crtimeId 
											";
											
									queryObj = new Query( datasource=application.dnsMaster ) ;
									queryObj.addParam( name="crtimeId", value=target[8], cfsqltype="cf_sql_char" );
									rs_clsroomExamTimeTable= queryObj.execute( sql=sql ).getResult();
									
									
									sql = "UPDATE t_clsroom_forresit a 
												SET a.course_count = :CourseCount ,
													a.sits_left = :SitLeft ,
													a.exam_courses = :ExamCourses ,
													a.sits_used = :SitUsed 
												WHERE a.crtime_id = :crtimeId ";
												
									queryObj = new Query( datasource=application.dnsMaster ) ;
									queryObj.addParam( name="CourseCount", value=target[6], cfsqltype="cf_sql_integer" );
									queryObj.addParam( name="SitLeft", value=target[4], cfsqltype="cf_sql_integer" );
									queryObj.addParam( name="ExamCourses", value=target[11], cfsqltype="cf_sql_varchar" );
									queryObj.addParam( name="SitUsed", value=rs_classroom.sits_exam - rs_clsroomExamTimeTable.sits_left, cfsqltype="cf_sql_integer" );
									queryObj.addParam( name="crtimeId", value=target[8], cfsqltype="cf_sql_char" );
									queryObj.execute( sql=sql );
								
									sql = "DELETE 
											FROM t_clsroom_timetable a 
											WHERE a.crtime_id = :taskId ";
																				
									queryObj = new Query( datasource=application.dnsMaster ) ;
									queryObj.addParam( name="taskId", value=nextCombo[8], cfsqltype="cf_sql_char" );
									queryObj.execute( sql=sql );
									
									/* 第二步 更新学生考试场地信息 */
									sql = "UPDATE t_student_resit a 
											SET a.cr_id = : Classroom 
											WHERE a.task_exrstid = :examId ";
											
									queryObj = new Query( datasource=application.dnsMaster ) ;
									queryObj.addParam( name="examId", value=nextCombo[7], cfsqltype="cf_sql_varchar" );
									
									queryObj.addParam( name="Classroom", value=target[9], cfsqltype="cf_sql_char" );
									queryObj.execute( sql=sql );
									
								}
								else {
									/* 达到考场门数上限 */
									arrayAppend( progress, target[10] & " 达到课程门数上限" );
								
									arrayAppend( rooms, [ nextCombo[3], nextCombo[5], nextCombo[10], "" ] );

									target = usageArray[i];							
								}
							
							}
							else {
								/* 考场座位数不够 合并目标下移 */
								arrayAppend( progress, target[10] & " 考试座次不足" );
							
								arrayAppend( rooms, [ nextCombo[3], nextCombo[5], nextCombo[10], "" ] );

								target = usageArray[i];
							}
						
						}
						else {
							/* 校区 不同 合并目标下移 */
							arrayAppend( progress, target[3] & " 与 " & nextCombo[3] & " 考试校区 不一致" );
						
							arrayAppend( rooms, [ nextCombo[3], nextCombo[5], nextCombo[10], "" ] );
						
							target = usageArray[i];
						}
					
					}
					else {
						/* 任课单位 不同 合并目标下移 */
						arrayAppend( progress, target[3] & " 与 " & nextCombo[3] & " 任课单位 不一致" );
					
						arrayAppend( rooms, [ nextCombo[3], nextCombo[5], nextCombo[10], "" ] );
					
						target = usageArray[i];
					}
				
				}
			
			}

		}
		else {
		
		}
			
					

		structInsert(response, "Progress", progress, true);
		structInsert(response, "ClassRoom", rooms, true);

		structInsert(response, "Usages", usageArray, true);
		
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>
